#ifndef JVIDEOVIEW_H
#define JVIDEOVIEW_H
#include<fstream>
#include <mutex>
struct  AVFrame;
void MSleep(int ms);
long long NowMs();
class JVideoView
{
public:
    JVideoView();
    enum Format//和ffmpeg中的枚举值一致
    {
        YUV420P=0,
        ARGB = 25,
        RGBA = 26,
        BGRA = 28
    };

    enum RenderType
    {
        SDL = 0
    };

    static JVideoView *Create(RenderType type=SDL);
    /////////////////////////////////////////////////
    /// \brief Init 初始化渲染窗口
    /// \param w    窗口宽度
    /// \param h    窗口高度
    /// \param fmt  绘制的像素格式
    /// \param win_id 窗口句柄，如果为空就创建新窗口
    /// \return
    ///
    virtual bool Init(int w,int h,Format fmt=RGBA)=0;

    ///////////////////////////////////////////////////
    /// \brief Draw      渲染图像
    /// \param data      渲染的二进制数据
    /// \param linesize   一行数据的字节数，对于yuv420p就是一行y的字节数
    /// \return
    ///
    virtual bool Draw(const unsigned char* data,int linesize=0)=0;
    virtual bool Draw(const unsigned char* y,int y_pitch,
                      const unsigned char* u,int u_pitch,
                      const unsigned char* v,int v_pitch)=0;


    virtual bool isExit() = 0;
    virtual void Close()=0;


    bool DrawFrame(AVFrame* frame);

    void Scale(int w,int h)
    {
        scale_h_ = h;
        scale_w_ = w;
    }

    void set_win_id(void* win){win_id_ = win;}
    bool Open(std::string filepath);

    ///////////////////////////////////////////////////
    /// \brief Read 读取一帧数据，并且维护AVFrame
    /// \return
    ///
    AVFrame* Read();


    int render_fps(){return render_fps_;}
protected:
    void* win_id_ = nullptr;
    int render_fps_ =0;
    int width_ = 0;
    int height_ = 0;
    Format fmt_ = RGBA;
    std::mutex mtx_;
    int scale_w_=0; //显示大小
    int scale_h_=0;
    long long beg_ms_ = 0;
    int count_ = 0;
private:
    std::ifstream ifs_;
    AVFrame* frame_ = nullptr;
};

#endif // JVIDEOVIEW_H
